﻿namespace WMS6.Web
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web;
    using WMS6.Web.MessageServiceRef;

    public class MessageServiceClientProxy : BasePage
    {
        public string DeleteSendMessage(List<string> userMessageIDS)
        {
            PubParasInfo pubParasInfo = this.InitPubParasInfo();
            string result = string.Empty;
            MessageServiceClient client = new MessageServiceClient("BasicHttpBinding_IMessageService");
            try
            {
                result = client.DeleteSendMessage(userMessageIDS, pubParasInfo);
                client.Close();
            }
            catch (Exception ex)
            {
                client.Abort();
                SysLog.GetInstance().LogProxyError(ex);
            }
            return result;
        }

        public string DeleteUserMessageInfo(List<string> userMessageIDS)
        {
            PubParasInfo pubParasInfo = this.InitPubParasInfo();
            string result = string.Empty;
            MessageServiceClient client = new MessageServiceClient("BasicHttpBinding_IMessageService");
            try
            {
                result = client.DeleteUserMessage(userMessageIDS, pubParasInfo);
                client.Close();
            }
            catch (Exception ex)
            {
                client.Abort();
                SysLog.GetInstance().LogProxyError(ex);
            }
            return result;
        }

        public DataSet GetAllUnreadUserMessageInfo()
        {
            PubParasInfo pubParasInfo = this.InitPubParasInfo();
            DataSet ds = null;
            MessageServiceClient client = new MessageServiceClient("BasicHttpBinding_IMessageService");
            try
            {
                ds = client.GetAllUnreadUserMessage(pubParasInfo);
                client.Close();
            }
            catch (Exception ex)
            {
                client.Abort();
                SysLog.GetInstance().LogProxyError(ex);
            }
            return ds;
        }

        public DataSet GetAllUserMessageInfo()
        {
            PubParasInfo pubParasInfo = this.InitPubParasInfo();
            DataSet ds = null;
            MessageServiceClient client = new MessageServiceClient("BasicHttpBinding_IMessageService");
            try
            {
                ds = client.GetUserMessageAllInfo(pubParasInfo);
                client.Close();
            }
            catch (Exception ex)
            {
                client.Abort();
                SysLog.GetInstance().LogProxyError(ex);
            }
            return ds;
        }

        public DataSet GetInMessageInfoByQuery(UserMessageQueryEntity userMessageQuery)
        {
            PubParasInfo pubParasInfo = this.InitPubParasInfo();
            DataSet ds = null;
            MessageServiceClient client = new MessageServiceClient("BasicHttpBinding_IMessageService");
            try
            {
                ds = client.GetInMessageInfoByQueryEntity(userMessageQuery, pubParasInfo);
                client.Close();
            }
            catch (Exception ex)
            {
                client.Abort();
                SysLog.GetInstance().LogProxyError(ex);
            }
            return ds;
        }

        public static MessageServiceClientProxy GetInstance()
        {
            return new MessageServiceClientProxy();
        }

        private UserUnreadMessageInfo getUnreadMessageByID(DataTable dt, string userID)
        {
            UserUnreadMessageInfo userUnreadmessage = null;
            if ((dt != null) && (dt.Rows.Count > 0))
            {
                DataView view = dt.DefaultView;
                view.RowFilter = " TO_USER_ID='" + userID + "'";
                if (view.Count > 0)
                {
                    userUnreadmessage = new UserUnreadMessageInfo {
                        RowID = int.Parse(view.Table.Rows[0]["ROWID"].ToString()),
                        FromUserID = view.Table.Rows[0]["FROM_USER_ID"].ToString(),
                        MessageTitle = view.Table.Rows[0]["MESSAGE_TITLE"].ToString(),
                        MessageContent = view.Table.Rows[0]["MESSAGE_CONTENT"].ToString(),
                        UpdatedDate = DateTime.Parse(view.Table.Rows[0]["UPDATED_DATE"].ToString()),
                        UnreadQty = view.Count
                    };
                }
            }
            return userUnreadmessage;
        }

        public DataSet GetUserMessageInfoByQuery(UserMessageQueryEntity userMessageQuery)
        {
            PubParasInfo pubParasInfo = this.InitPubParasInfo();
            DataSet ds = null;
            MessageServiceClient client = new MessageServiceClient("BasicHttpBinding_IMessageService");
            try
            {
                ds = client.GetUserMessageInfoByQueryEntity(userMessageQuery, pubParasInfo);
                client.Close();
            }
            catch (Exception ex)
            {
                client.Abort();
                SysLog.GetInstance().LogProxyError(ex);
            }
            return ds;
        }

        public UserMessageInfo GetUserMessageInfoByQueryEntity(string userMessageID)
        {
            PubParasInfo pubParasInfo = this.InitPubParasInfo();
            UserMessageInfo getUserMessageInfoByQueryEntity = null;
            MessageServiceClient client = new MessageServiceClient("BasicHttpBinding_IMessageService");
            try
            {
                getUserMessageInfoByQueryEntity = client.GetUserMessageInfoByID(userMessageID, pubParasInfo);
                client.Close();
            }
            catch (Exception ex)
            {
                client.Abort();
                SysLog.GetInstance().LogProxyError(ex);
            }
            return getUserMessageInfoByQueryEntity;
        }

        public UserUnreadMessageInfo GetUserUnreadMessageByID(string userID)
        {
            DataSet ds = null;
            UserUnreadMessageInfo userUnreadmessage = null;
            if ((LicenseUtils.messageTable == null) || LicenseUtils.IsUnreadMessageChange)
            {
                ds = this.GetAllUnreadUserMessageInfo();
                if (ds != null)
                {
                    LicenseUtils.messageTable = ds.Tables[0];
                    userUnreadmessage = this.getUnreadMessageByID(LicenseUtils.messageTable, userID);
                }
                LicenseUtils.IsUnreadMessageChange = false;
                return userUnreadmessage;
            }
            return this.getUnreadMessageByID(LicenseUtils.messageTable, userID);
        }

        private PubParasInfo InitPubParasInfo()
        {
            if (base.UserInfo != null)
            {
                return new PubParasInfo { UserID = base.UserInfo.UserID, UserLanguage = base.UserInfo.Preference.Lang, CurrentWH = base.UserInfo.CurrentWH };
            }
            HttpContext.Current.Response.Redirect("../Login.aspx?from=Logout", true);
            return null;
        }

        public string InsertUserMessageInfo(UserMessageInfo userMessageInfo)
        {
            PubParasInfo pubParasInfo = this.InitPubParasInfo();
            string result = string.Empty;
            MessageServiceClient client = new MessageServiceClient("BasicHttpBinding_IMessageService");
            try
            {
                result = client.InsertUserMessageInfo(userMessageInfo, pubParasInfo);
                client.Close();
            }
            catch (Exception ex)
            {
                client.Abort();
                SysLog.GetInstance().LogProxyError(ex);
            }
            return result;
        }

        public string InsertUserMessageInfoList(List<UserMessageInfo> userMessageInfoList)
        {
            PubParasInfo pubParasInfo = this.InitPubParasInfo();
            string result = string.Empty;
            MessageServiceClient client = new MessageServiceClient("BasicHttpBinding_IMessageService");
            try
            {
                result = client.InsertUserMessageInfoList(userMessageInfoList, pubParasInfo);
                client.Close();
            }
            catch (Exception ex)
            {
                client.Abort();
                SysLog.GetInstance().LogProxyError(ex);
            }
            return result;
        }

        public string UpdateMarkUserMessageInfo(string rowIDs, string isRead)
        {
            PubParasInfo pubParasInfo = this.InitPubParasInfo();
            string result = string.Empty;
            MessageServiceClient client = new MessageServiceClient("BasicHttpBinding_IMessageService");
            try
            {
                result = client.UpdateMarkUserMessageInfo(rowIDs, isRead, pubParasInfo);
                client.Close();
            }
            catch (Exception ex)
            {
                client.Abort();
                SysLog.GetInstance().LogProxyError(ex);
            }
            return result;
        }

        public string UpdateUserMessageInfo(UserMessageInfo userMessageInfo)
        {
            PubParasInfo pubParasInfo = this.InitPubParasInfo();
            string result = string.Empty;
            MessageServiceClient client = new MessageServiceClient("BasicHttpBinding_IMessageService");
            try
            {
                result = client.UpdateUserMessageInfo(userMessageInfo, pubParasInfo);
                client.Close();
            }
            catch (Exception ex)
            {
                client.Abort();
                SysLog.GetInstance().LogProxyError(ex);
            }
            return result;
        }
    }
}

